package com.example.damaimaven.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.damaimaven.entity.User;
import com.example.damaimaven.mapper.UserMapper;
import com.example.damaimaven.service.BizException;
import com.example.damaimaven.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zzh
 * @since 2024-07-09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    @Transactional
    public User login(User user) throws BizException {
        //判断输入是否正确
        if (user.getUsername() == null || user.getUsername().isBlank()) {
            throw new BizException("请输入用户名!");
        }

        if (user.getPassword() == null || user.getPassword().isBlank()) {
            throw new BizException("请输入密码!");
        }
        if(user.getPassword().length() < 6) {
            throw new BizException("密码长度必须大于等于6!");
        }
        //TODO 请自行扩展其他字段验证

        //数据是否正确
//        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(User::getUsername, user.getUsername());
//        if (this.count(lqw)>0) {
//            throw new BizException("该用户名已被注册!");
//        }
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername, user.getUsername());
        lqw.eq(User::getPassword, user.getPassword());
        User one = getOne(lqw);
        if (one==null) {
            throw new BizException("用户名或密码错误!");
        }
        //返回用户对象
        return one;

        }
    }